<?php
/**
 * 天网行动控制器
 * @author jianhui
 *
 */
class SingleNet extends MY_Controller{
    function SingleNet(){
        parent::MY_Controller();
    }
    /**
     * 天网行动添加或修改
     */
    function add(){
        $this->load->model('singleNetModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'   => $this->user['userId'],
                'procId'    => (int)$this->input->post('procId'),
                'isBubao'   => (int)$this->input->post('isBubao'),
                'theme'     => $this->input->post('theme'),
                'actionType'=> $this->input->post('actionType'),
                'mediaName' => $this->input->post('mediaName'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'frequency' => (int)$this->input->post('frequency'),
                'budgetApply' => (float)$this->input->post('budgetApply'),
                'budgetSubTime' => time(),
                'budgetAudit' => (float)$this->input->post('budgetApply'),
                'budgetAuditRole' => 0,
                'budgetAuditRs' => 1,
                //'settleApply'   => (float)$this->input->post('budgetApply'),
            );
            $data['procId'] || showError('请选择主线流程');
            $data['theme'] || showError('请输入项目主题');
            $data['actionType'] || showError('请选择标准动作');
            $data['mediaName'] || showError('请输入媒体名称');
            $data['startTime'] || showError('请选择开始时间');
            $data['endTime'] || showError('请选择结束时间');
            $data['frequency'] || showError('请输入发布频次');
            $data['budgetApply'] >= 0 || showError('请输入预算费用');
            if ($id){
                $id = $this->singleNetModel->edit($id, $data);
            }else{
                if ($this->singleNetModel->getData(array('storeId'=>$data['storeId'],'procId'=>$data['procId']))){
                    showError('您已经添加天网行动');
                }
                $data['createTime'] = time();
                $id = $this->singleNetModel->add($data);
            }
            if ($id){
                showSuccess('保存天网行动成功');
            }else{
                showError('保存天网行动失败');
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $id = (int)$this->input->get('id');
            $isBubao = (int)$this->input->get('isBubao');
            if ($id){
                $netData = $this->singleNetModel->getData($id);
                $netData || showError('您要修改的天网行动数据不存在');
                in_array($netData['settleAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
                $procId = $netData['procId'];
                $isBubao = $netData['isBubao'];
            }
            $procId || showError('请选择要添加项目的流程');
            $this->viewData['procId'] = $procId;
            $this->viewData['isBubao'] = $isBubao;
            //主线流程数据
            $this->load->model('singleProcessModel');
            $procData = $this->singleProcessModel->getData($this->viewData['procId']);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            //报表金额
            $this->load->model('importMoneyModel');
            $netData['budgetApply'] = $this->importMoneyModel->getMoney($this->user['userId'], $procData['timeUnit'], 12, $procData['year']);
            $this->viewData['netData'] = $netData;
            $this->load->view('SingleNet/add', $this->viewData);
        }
    }
    /**
     * 天网行动删除
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的天网行动');
        $this->load->model('singleNetModel');
        if ($this->singleNetModel->del(array('id'=>$id,'storeId'=>$this->user['userId']) )){
            showSuccess('删除天网行动成功');
        }else{
            showError('删除天网行动失败');
        }
    }
    /**
     * 天网行动结算申请
     */
    function settleSubmit(){
        $this->load->model('singleNetModel');
        if ($_POST){
            $netData = $this->input->post('net');
            $netData || showError('您的结算申请数据为空');
            if ($netData){
                foreach ($netData as $k => $v){
                    if ((int)$v['isSettle']){
                        (float)$v['settleApply'] >= 0 || showError('编号为【'.$k.'】的天网行动实际费用为空');
                        (int)$v['fileId'] || showError('编号为【'.$k.'】的天网行动自证照片为空');
                    }
                }
            }
            if ($netData){
                foreach ($netData as $k => $v){
                    $this->singleNetModel->edit((int)$k, array(
                        'settleAuditRole' => 0,
                        'settleAuditRs'=>(int)$v['isSettle'] ? 0 : -1,
                        'isSettle'=>(int)$v['isSettle'],
                        'fileId' => (int)$v['fileId'],
                        'settleApply' => (float)$v['settleApply'],
                        'settleSubTime' => time()
                    ));
                }
            }
            showSuccess('提交结算申请成功');
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要申请结算的天网行动');
            $this->viewData['netData'] = $this->singleNetModel->getNewData(array('id'=>$id,'storeId'=>$this->user['userId']));
            $this->viewData['netData'] || showError('天网行动数据不存在');
            $this->viewData['action'] = 'edit';            
            $this->load->view('SingleNet/settleSubmit', $this->viewData);
        }
    }
}